package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ReportVenueSpaceAuditDTO;
import com.eastfair.venueservice.dto.ReportVenueSpaceAuditPageQuery;
import com.eastfair.venueservice.dto.ReportVenueSpaceAuditSaveDTO;
import com.eastfair.venueservice.dto.ReportVenueSpaceAuditUpdateDTO;
import com.eastfair.venueservice.entity.ReportVenueSpaceAudit;
import com.eastfair.venueservice.service.ReportVenueSpaceAuditService;
import com.eastfair.venueservice.vo.ReportVenueSpaceAuditVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 一键报馆审核记录
 * </p>
 *
 * @author dq
 * @date 2022-09-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportVenueSpaceAudit")
@Api(value = "ReportVenueSpaceAudit", tags = "一键报馆审核记录")
@PreAuth(replace = "venueservice:reportVenueSpaceAudit:")
public class ReportVenueSpaceAuditController extends SuperController<ReportVenueSpaceAuditService, Long, ReportVenueSpaceAudit, ReportVenueSpaceAuditPageQuery, ReportVenueSpaceAuditSaveDTO, ReportVenueSpaceAuditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportVenueSpaceAuditService reportVenueSpaceAuditService;

    @Override
    public void handlerResult(IPage<ReportVenueSpaceAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportVenueSpaceAudit> reportVenueSpaceAuditList = list
                .stream()
                .map((map) -> ReportVenueSpaceAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportVenueSpaceAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportVenueSpaceAuditPageQuery> params) {
        ReportVenueSpaceAuditPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询报馆审核VO列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询报馆审核VO列表")
    @PostMapping("/page/reportVenueSpaceAudit")
    public R<IPage<ReportVenueSpaceAuditVO>> queryReportVenueSpaceAudit(@RequestBody PageParams<ReportVenueSpaceAuditDTO> pageParams) {
        log.info("queryReportVenueSpaceAudit - 分页查询报馆审核VO列表, pageParams={}", pageParams);
        return R.successDef(reportVenueSpaceAuditService.listReportVenueSpaceAuditVOs(pageParams));
    }

    /**
     * 一键报馆编号查询报馆审核记录.
     *
     * @param reportVenueSpaceNumber the report venue space number
     * @return the by report venue space number
     */
    @ApiOperation("一键报馆编号查询报馆审核记录")
    @GetMapping("/get/reportVenueSpaceNumber/{reportVenueSpaceNumber}")
    public R<ReportVenueSpaceAuditVO> getByReportVenueSpaceNumber(@PathVariable("reportVenueSpaceNumber") String reportVenueSpaceNumber) {
        log.info("getByReportVenueSpaceNumber - 一键报馆编号查询报馆审核记录, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        return R.successDef(reportVenueSpaceAuditService.getVOByReportVenueSpaceNumber(reportVenueSpaceNumber));
    }

    /**
     * 提交一键报馆审核记录.
     *
     * @param reportVenueSpaceAuditDTO the report venue space audit dto
     * @return the r
     */
    @ApiOperation("提交一键报馆审核记录")
    @PostMapping("/submit")
    public R<Boolean> submit(@RequestBody ReportVenueSpaceAuditDTO reportVenueSpaceAuditDTO) {
        log.info("submit - 提交一键报馆审核记录, reportVenueSpaceAuditDTO={}", reportVenueSpaceAuditDTO);
        return R.successDef(reportVenueSpaceAuditService.submitReportVenueSpaceAudit(reportVenueSpaceAuditDTO));
    }

    /**
     * 更新审核结果.
     *
     * @param reportVenueSpaceAuditDTO the report venue space audit dto
     * @return the r
     */
    @ApiOperation("更新审核结果")
    @PostMapping("/update/auditStatus")
    public R<Boolean> updateAuditStatus(@RequestBody ReportVenueSpaceAuditDTO reportVenueSpaceAuditDTO) {
        log.info("updateAuditStatus - 更新审核结果, reportVenueSpaceAuditDTO={}", reportVenueSpaceAuditDTO);
        return R.successDef(reportVenueSpaceAuditService.updateAuditStatus(reportVenueSpaceAuditDTO));
    }
}
